package org.itboys.mobile.controller.mobile.collection;

import com.google.common.collect.Maps;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.ErrorException;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.kitchen.util.MobileSignUtils;
import org.itboys.mobile.dto.collection.KitchenCollectionDto;
import org.itboys.mobile.service.mongo.mobile.collection.SundayMobileCollectionService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017年5月10日
 * 描述：花果山（农厨）_移动端_收藏_controller
 */
@Controller
@RequestMapping("/kitchen/mobile/collection")
public class KitchenMobileCollectionController extends BaseController{
    @Autowired
    private SundayMobileCollectionService collectionService;

    /**
     * 查询用户所有收货地址
     * @param request
     * @param response
     * @param type
     * @param pageSize
     * @param pageNumber
     * @param sort
     * @param order
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/select", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】获取收藏的物品。商品，菜谱，资讯。都要分页")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required=true, name="type", value="收藏的类型 1商品，2菜谱，3资讯")@RequestParam(value = "type", required = true)Integer type,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){



            try{




                Long memberId = AccountIdHoldUtils.getMemberId();
                List<Map<String,Object>> result =collectionService.selectCollection(request,memberId,type);
                ResponseMessage.success(result);
            }catch (Exception e){
                String msg =CommonConstants.ERROMSG;
                Integer code=CommonConstants.FAIL_CODE;
                //自定义异常（签名错误）
                if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                    msg=e.getMessage();
                //token已过期
                }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                    code=CommonConstants.TOKEN_CODE;
                    msg=CommonConstants.TOKENMSG;
                }else{
                    logger.error("/kitchen/mobile/collection/select----e="+e.getMessage());
                }
                e.printStackTrace();
                ResponseMessage.error(code,msg);
            }
    }
    /**
     * 收藏或者取消收藏
     * @param request
     * @param response
     * @param type
     * @param otherId
     * @param nonce
     * @param sign
     */
    @RequestMapping(value="/save", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】收藏或者取消收藏")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ApiParam(required=true, name="type", value="收藏类型 1商品，2菜谱，3资讯")@RequestParam(value = "type", required = true)Integer type,
                     @ApiParam(required=true, name="otherId", value="被收藏（商品，菜谱，资讯）的id")@RequestParam(value = "otherId", required = true)Long otherId,
                     @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                     @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
            try{
                //2017年6月20日校验签名
               /* Map<String, Object> param = Maps.newHashMapWithExpectedSize(4);
                param.put("type",type);
                param.put("otherId",otherId);
                param.put("nonce", nonce);
                param.put("sign", sign);
                boolean signSer = MobileSignUtils.signCheck(param, sign);
                if(!signSer){
                    logger.warn("签名校验失败");
                    ResponseMessage.error(-1,"签名校验失败");
                    return;
                }*/


                Long memberId = AccountIdHoldUtils.getMemberId();
                collectionService.collection(memberId,type,otherId);
                ResponseMessage.success();
            }catch (Exception e){
                String msg =CommonConstants.ERROMSG;
                Integer code=CommonConstants.FAIL_CODE;
                //自定义异常（签名错误）
                if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                    msg=e.getMessage();
                    //token已过期
                }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                    code=CommonConstants.TOKEN_CODE;
                    msg=CommonConstants.TOKENMSG;
                }else{
                    logger.error("/kitchen/mobile/collection/save----e="+e.getMessage());
                }
                e.printStackTrace();
                ResponseMessage.error(code,msg);
            }

    }
    /**
     * 收藏或者取消收藏web（网页端，针对资讯）
     * @param request
     * @param response
     * @param type
     * @param otherId

     */
    @RequestMapping(value="/web/save", method= RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】收藏或者取消收藏")
    public void web_save(HttpServletRequest request,
                     HttpServletResponse response,
                         @ApiParam(required=true, name="memberId", value="用户ID")@RequestParam(value = "memberId", required = true)Long memberId,
                         @ApiParam(required=true, name="type", value="收藏类型 1商品，2菜谱，3资讯")@RequestParam(value = "type", required = true)Integer type,
                     @ApiParam(required=true, name="otherId", value="被收藏（商品，菜谱，资讯）的id")@RequestParam(value = "otherId", required = true)Long otherId
                    ){
        try{


            collectionService.collectionWeb(memberId,type,otherId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/kitchen/mobile/collection/web/save----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }

    }
}
